import { userInfo } from "os";
import { Logs } from "src/logs/logs.entity";
import { Roles } from "src/roles/roles.entity";
import { Column, Entity, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import { UserProfile } from "./userProfile.entity";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;
    
    @Column()   
    username: string;
    
    @Column()
    password: string;


    // 一个用户可以有多个日志， 一对多
    // 第二个参数是为了 关联Logs实体中的 user 字段
    @OneToMany(() => Logs, (logs) => logs.user)
    logs: Logs[];

    @ManyToMany(() => Roles, (role) => role.users)
    @JoinTable({
        name: 'users_roles',
        joinColumn: {
            name: 'useId',
            referencedColumnName: 'id',
        },
        inverseJoinColumn: {
            name: 'roleId',
            referencedColumnName: 'id',
        },
    })
    roles: Roles[];



    @OneToOne(() => UserProfile, (profile) => profile.user)
    profile: UserProfile;
}